package com.personal.website.lottery.web.util;

import com.personal.website.lottery.table.*;
import com.personal.website.lottery.web.model.DoubleColorBall;
import com.personal.website.lottery.web.service.DoubleColorBallService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Gavin on 2016/2/17.
 */
public class TableRecordsGenerate {

    private final Logger logger = LoggerFactory.getLogger(TableRecordsGenerate.class);
    private DoubleColorBallService doubleColorBallService;

    public TableRecordsGenerate(DoubleColorBallService doubleColorBallService) {
        this.doubleColorBallService = doubleColorBallService;
    }

    private List<Integer> getRedBalls(DoubleColorBall ball) {
        List<Integer> redBalls = new ArrayList<Integer>();
        redBalls.add(Integer.parseInt(ball.getRedOne()));
        redBalls.add(Integer.parseInt(ball.getRedTwo()));
        redBalls.add(Integer.parseInt(ball.getRedThree()));
        redBalls.add(Integer.parseInt(ball.getRedFour()));
        redBalls.add(Integer.parseInt(ball.getRedFive()));
        redBalls.add(Integer.parseInt(ball.getRedSix()));
        return redBalls;
    }

    public String generateBlueTableRecords(int maxSize) {
        return generatTableRecords(BlueTable.class, maxSize);
    }

    public String generateEightDigitTableRecords(int maxSize) {
        return generatTableRecords(EightDigitTable.class, maxSize);
    }

    public String generateElevenDigitTableRecords(int maxSize) {
        return generatTableRecords(ElevenDigitTable.class, maxSize);
    }

    public String generateEnd3dTableRecords(int maxSize) {
        return generatTableRecords(End3dTable.class, maxSize);
    }

    public String generateEndTableRecords(int maxSize) {
        return generatTableRecords(EndTable.class, maxSize);
    }

    public String generateEvenTableRecords(int maxSize) {
        return generatTableRecords(EvenTable.class, maxSize);
    }

    public String generateFourPart012TableRecords(int maxSize) {
        return generatTableRecords(FourPart012Table.class, maxSize);
    }

    public String generateFourPartEnd012TableRecords(int maxSize) {
        return generatTableRecords(FourPartEnd012Table.class, maxSize);
    }

    public String generateMainTableRecords(int maxSize) {
        return generatTableRecords(MainTable.class, maxSize);
    }

    public String generateOddTableRecords(int maxSize) {
        return generatTableRecords(OddTable.class, maxSize);
    }

    public String generatTableRecords(Class clazz, int maxSize) {
        String ballRecords = "";
        try {
            Method method = clazz.getMethod("newInstance", Integer.class, List.class, Integer.class);
            for (DoubleColorBall ball : this.doubleColorBallService.getBalls(maxSize)) {
                ballRecords += method.invoke(null, ball.getIssue(), getRedBalls(ball), Integer.parseInt(ball.getBlue())).toString() + "$";
            }
        } catch (Exception e) {
            logger.error("反射调用generatTableRecords方法时异常！", e);
        }
        return ballRecords.substring(0, ballRecords.length() - 1);
    }


}
